//
//  SNHealthyCheckVc.m
//  project
//
//  Created by 黄建华 on 2017/12/26.
//  Copyright © 2017年 黄建华. All rights reserved.
//

#import "SNHealthyCheckVc.h"
#import "ZZCACircleProgress.h"
#define CHECKTIME 60.0
#import "SNHeartDetailTableViewCell.h"
#import "SNOxDetailTableViewCell.h"
#import "SNBpDetailTableViewCell.h"
#import "SNHearDetailVc.h"
#import "SNOxDetailVc.h"
#import "SNBpDetailVc.h"

@interface SNHealthyCheckVc ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) SNHealthySelectTypeView *selectView;
@property (nonatomic, strong) ZZCACircleProgress      *stepCircle;  // 步数圆圈
@property (nonatomic, assign) NSInteger               count;
@property (nonatomic, strong) NSTimer                 *timer;
@property (nonatomic, strong) UILabel                 *valueLabel;
@property (nonatomic, strong) UITableView             *tableView;
@property (nonatomic, strong) NSMutableArray          *modelArray;
@property (nonatomic, strong) UIButton                *nowDateBtn;
@property (nonatomic, strong) UIView                  *chooseDateView;
@property (nonatomic, strong) UILabel                 *checkLabelInfo;
@property (nonatomic, assign) CheckType               type;

@end

@implementation SNHealthyCheckVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    DEF_WEAKSELF_(SNHealthyCheckVc)
    
    _selectView = [[SNHealthySelectTypeView alloc] initWithFrame:SNFRAME((HHHWIDTH - 200)/2.0, 33 + 5 + FitSimpleIpx(0, 20), 200, 33)];
    [self.view addSubview:_selectView];
    
    UIButton *stepDetailButton = [UIButton buttonWithType:UIButtonTypeCustom];
    stepDetailButton.frame = SNFRAME(5, 30 + FitSimpleIpx(0, 20), 50, 50);
    [stepDetailButton setImageNormal:@"进入"];
    stepDetailButton.center = CGPointMake(25, _selectView.center.y);
    [stepDetailButton addTarget:self
                         action:@selector(backView)
               forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:stepDetailButton];
    
    _checkLabelInfo               = [[UILabel alloc] init];
    _checkLabelInfo.frame         = SNFRAME(5, _selectView.center.y +30, 220, 20);
    _checkLabelInfo.textColor     = SNCOLORBLACK;
    _checkLabelInfo.font          = SNFONT(17.0);
    _checkLabelInfo.textAlignment = NSTextAlignmentLeft;
    _checkLabelInfo.text          = [NSString stringWithFormat:@"%@ %d", SN_Text(@"体检时长:"), SN_USER.checkTime];
    [self.view addSubview:_checkLabelInfo];
    
    _nowDateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_nowDateBtn setTitleNormal:[NSString stringWithFormat:@"%d", SN_USER.checkTime]];
    _nowDateBtn.frame  = SNFRAME(5, 30 + FitSimpleIpx(0, 20) + 40, 70, 40);
    _nowDateBtn.center = CGPointMake(25 + 15, _selectView.center.y + 80);
    [_nowDateBtn setTitleColorNormal:SNCOLORBLACK];
    _nowDateBtn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
    [_nowDateBtn addTarget:self action:@selector(showChooseDateView) forControlEvents:UIControlEventTouchUpInside];
    _nowDateBtn.backgroundColor = SNCOLORGRAYLINE;
    [self.view addSubview:_nowDateBtn];
    
    UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    shareBtn.frame = CGRectMake(screenWidth - 105, 35, 44, 44);
    [shareBtn setImageNormal:@"分享share"];
    shareBtn.center = CGPointMake(_selectView.center.x + _selectView.width /2.0 + 27, stepDetailButton.center.y);
    [shareBtn addTarget:self
                 action:@selector(shareButtonClick)
       forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:shareBtn];
    
    UIButton *detailBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    detailBtn.frame = CGRectMake(screenWidth - 44, 35, 44, 44);
    [detailBtn setImageNormal:@"数据"];
    detailBtn.imageEdgeInsets = UIEdgeInsetsMake(2, 2, 4, 2);
    detailBtn.center = CGPointMake(_selectView.center.x + _selectView.width /2.0 + 80, stepDetailButton.center.y);
    [detailBtn addTarget:self
                  action:@selector(detailButtonClick)
        forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:detailBtn];
    
    CGFloat xCrack    = ([UIScreen mainScreen].bounds.size.width)/3.0;
    CGFloat yCrack    = 100;
    CGFloat itemWidth = xCrack + yCrack;
    _stepCircle                  = [[ZZCACircleProgress alloc] initWithFrame:CGRectMake(xCrack - yCrack/2, yCrack, itemWidth, itemWidth) pathBackColor:nil pathFillColor:SNCOLORBLACK startAngle:-90 strokeWidth:4];
    _stepCircle.showProgressText = YES;
    _stepCircle.duration         = 0.0; //动画时长
    _stepCircle.increaseFromLast = YES;
    _stepCircle.pointImage.image = [UIImage imageNamed:@"circle_point2"];
    // _stepCircle.increaseFromLast = YES; // 如果是今天就从当前进度开始
    
    _stepCircle.progress        = 1.0;
    _stepCircle.ValueLabel.font = SNFONT(33);
    _stepCircle.unitLabel.font  = SNFONT(15);
    _stepCircle.unitLabel.text  = SN_Text(@"数值");
    _stepCircle.prepareToShow   = YES;//设置好属性，准备好显示了，显示之前必须调用一次
    [self.view addSubview:_stepCircle];
    
    UITapGestureRecognizer *stepCircletapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureHandle)];
    [_stepCircle addGestureRecognizer:stepCircletapGesture];
    
    UIButton *start = [[UIButton alloc] initWithFrame:SNFRAME(HHHWIDTH/2 - 130, _stepCircle.origin.y + _stepCircle.height + 20, 110, 44)];
    [start setTitle:SN_Text(@"开始") forState:UIControlStateNormal];
    [start setTitleColor:SNCOLORBLACK forState:UIControlStateNormal];
    [start setTitleColor:SNCOLORBLACK forState:UIControlStateHighlighted];
    [start addTarget:self action:@selector(startClick) forControlEvents:UIControlEventTouchUpInside];
    start.layer.borderWidth  = 1;
    start.layer.borderColor  = SNCOLORGRAY.CGColor;
    start.layer.cornerRadius = 6.0;
    [self.view addSubview:start];
    
    UIButton *end = [[UIButton alloc] initWithFrame:SNFRAME(HHHWIDTH/2 + 30, _stepCircle.origin.y + _stepCircle.height + 20, 110, 44)];
    [end setTitleColor:SNCOLORGRAY forState:UIControlStateHighlighted];
    [end setTitle:SN_Text(@"停止") forState:UIControlStateNormal];
    [end setTitleColor:SNCOLORBLACK forState:UIControlStateNormal];
    [end addTarget:self action:@selector(endClick) forControlEvents:UIControlEventTouchUpInside];
    end.layer.borderWidth  = 1;
    end.layer.borderColor  = SNCOLORGRAY.CGColor;
    end.layer.cornerRadius = 6.0;
    [self.view addSubview:end];
    
    _stepCircle.ValueLabel.text = @"--";
    
    UIView *lineView_2 = [[SNShadowView alloc] init];
    lineView_2.frame = CGRectMake(0, end.origin.y + 55, HHHWIDTH, 3);
    [self.view addSubview:lineView_2];
    
    _tableView                = [[UITableView alloc]initWithFrame:CGRectMake(0, end.origin.y + 60, HHHWIDTH, HHHHEIGHT * 0.40 -10) style:UITableViewStylePlain];
    _tableView.delegate       = self;
    _tableView.dataSource     = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //    _tableView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_tableView];
    
    [self selectType:CheckHeart];
    
    _selectView.selectType = ^(NSInteger type) {
        [weakSelf selectType:type];
    };
}

- (void)showChooseDateView {
    //    if (KK_BLESend.isSend)
    //    {
    //        SHOWMBProgressHUD(SN_Text(@"Updating"), nil, nil, NO, 2.0);
    //        return;
    //    }
    
    if (_selectView.isCheck) {
        SHOWMBProgressHUD(SN_Text(@"正在体检"), nil, nil, NO, 2.0);
        return;
    }
    
    if (_chooseDateView) {
        [_chooseDateView removeAllSubviews];
        _chooseDateView = nil;
    }
    _chooseDateView                 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
    _chooseDateView.backgroundColor = [UIColor clearColor];
    _chooseDateView.alpha           = 0;
    //点击背景隐藏
    UILabel *label = [[UILabel alloc]initWithFrame:_chooseDateView.frame];
    label.text                   = @"";
    label.backgroundColor        = [UIColor blackColor];
    label.alpha                  = 0.4;
    label.userInteractionEnabled = YES;
    [_chooseDateView addSubview:label];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenChooseDateView)];
    [label addGestureRecognizer:tap];
    NSArray *menuArr;
    if (SN_PublicClass.checkType == 3) {
        menuArr = @[@"60", @"80", @"100", @"120"];
    } else {
        menuArr = @[@"30", @"60", @"90", @"120"];
    }
    
    for (NSInteger i = 0; i < menuArr.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(_nowDateBtn.x, _nowDateBtn.totalHeight+i*40, 70, 40);
        button.tag   = i;
        [button setTitleNormal:menuArr[i]];
        button.backgroundColor = SNCOLORWHITE;
        [button addTarget:self action:@selector(dateChoose:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = [UIFont systemFontOfSize:12.0f];
        [button setTitleColorNormal:SNCOLORBLACK];
        [_chooseDateView addSubview:button];
        
        UIView *view = [[UIView alloc] init];
        view.frame           = SNFRAME(0, 0, button.width, 0.5);
        view.backgroundColor = SNCOLORGRAYLINE;
        [button addSubview:view];
    }
    
    [[UIApplication sharedApplication].keyWindow addSubview:_chooseDateView];
    [UIView animateWithDuration:0.2 animations:^{
        _chooseDateView.alpha = 1;
    }];
}

- (void)hiddenChooseDateView {
    [UIView animateWithDuration:0.2 animations:^{
        _chooseDateView.alpha = 0;
    } completion:^(BOOL finished) {
        [_chooseDateView removeFromSuperview];
    }];
}

- (void)dateChoose:(UIButton *)sender {
    NSLog(@"sender>>>>%d", sender.tag);
    [_nowDateBtn setTitleNormal:sender.titleLabel.text];
    SN_USER.checkTime = _nowDateBtn.titleLabel.text.integerValue;
    [SN_USER updateToDB];
    _checkLabelInfo.text = [NSString stringWithFormat:@"%@ %d", SN_Text(@"体检时长:"), SN_USER.checkTime];
    [self hiddenChooseDateView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _modelArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"SNHealthyCheckVcCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
        UIView *lineView = [[SNShadowView alloc] init];
        lineView.tag = indexPath.row;
        [cell addSubview:lineView];
    }
    NSString *value = [_modelArray objectAtIndex:indexPath.row];
    cell.textLabel.text = value;
    cell.textLabel.font = SNSFONT(15.0);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //    SNShadowView *view = (SNShadowView *)[cell viewWithTag:indexPath.row];
    //    view.frame = SNFRAME(0, 40, HHHWIDTH, 3);
    
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (void)startClick {
    if (_selectView.isCheck) {
        SHOWMBProgressHUD(SN_Text(@"正在体检"), nil, nil, NO, 2.0);
    } else {
        [self endClick];
        _selectView.isCheck            = YES;
        SN_PublicClass.isCheck         = YES;
        _timer                         = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(startTimerDown) userInfo:nil repeats:YES];
        _stepCircle.ValueLabel.text    = @"--";
        SN_PublicClass.heartCheckArray = [NSMutableArray array];
        SN_PublicClass.oXCheckArray    = [NSMutableArray array];
        SN_PublicClass.bpCheckArray    = [NSMutableArray array];
        [KK_BLESend sendHeartStatus:YES type:SN_PublicClass.checkType WithUpdate:^(id object, KKBLEAcceptType type) {
        }];
    }
}

- (void)startTimerDown {
    _count--;
    _stepCircle.progress = 1.0 *_count/SN_USER.checkTime;
    _checkLabelInfo.text = [NSString stringWithFormat:@"%@ %d", SN_Text(@"体检时长:"), _count];
    if (_count == 0) {
        [self endClick];
        SHOWMBProgressHUD(SN_Text(@"体检结束"), nil, nil, NO, 2.0);
        BOOL checkFinish = NO;
        // 心率
        if (SN_PublicClass.heartCheckArray.count > 0 && SN_PublicClass.checkType == 1) {
            int             total  = 0;
            NSMutableString *DataS = [NSMutableString string];
            for (NSString *heartS in SN_PublicClass.heartCheckArray) {
                total += heartS.intValue;
                [DataS appendString:heartS];
                [DataS appendString:@","];
            }
            heartSubModel *heartModel = [[heartSubModel alloc] init];
            heartModel.heartValue = total / SN_PublicClass.heartCheckArray.count;
            heartModel.count      = [NSDate getCurrentMinTotal]/15;
            int currentMin = [NSDate getCurrentMinTotal];
            heartModel.countMins = currentMin;
            NSDate *nowDate      = [NSDate date];//现在的UTC时间也可以写成[[NSDate alloc] init];
            NSDate *startOfToday = [[NSCalendar currentCalendar] startOfDayForDate:nowDate];
            
            NSDate   *date = [startOfToday dateByAddingTimeInterval:currentMin *60];
            NSString *time = [date dateToString];
            heartModel.updateTime = time;
            //            heartModel.data = DataS;
            
            HeartModel     *tmodel     = SN_PublicClass.todayHeart;
            NSMutableArray *checkArray = [NSMutableArray arrayWithArray:tmodel.heartSuArrayCheck];
            [checkArray addObject:heartModel];
            tmodel.heartSuArrayCheck = [NSMutableArray arrayWithArray:checkArray];
            [tmodel updateToDB];
            [HeartModel updateCheckHeart];
            [self selectType:CheckHeart];
            checkFinish = YES;
            _type       = CheckHeart;
        }
        
        // 血氧
        if (SN_PublicClass.oXCheckArray.count > 0 && SN_PublicClass.checkType == 2) {
            int             total  = 0;
            NSMutableString *DataS = [NSMutableString string];
            for (NSString *oxS in SN_PublicClass.oXCheckArray) {
                total += oxS.intValue;
                [DataS appendString:oxS];
                [DataS appendString:@","];
            }
            oxSubModel *oxModel = [[oxSubModel alloc] init];
            oxModel.oxValue = total / SN_PublicClass.oXCheckArray.count;
            oxModel.count   = [NSDate getCurrentMinTotal]/15;
            int currentMin = [NSDate getCurrentMinTotal];
            oxModel.countMins = currentMin;
            NSDate *nowDate      = [NSDate date];//现在的UTC时间也可以写成[[NSDate alloc] init];
            NSDate *startOfToday = [[NSCalendar currentCalendar] startOfDayForDate:nowDate];
            
            NSDate   *date = [startOfToday dateByAddingTimeInterval:currentMin *60];
            NSString *time = [date dateToString];
            oxModel.updateTime = time;
            //            oxModel.data       = DataS;
            
            OxModel        *tmodel     = SN_PublicClass.todayOx;
            NSMutableArray *checkArray = [NSMutableArray arrayWithArray:tmodel.oxsuArrayAuto];
            [checkArray addObject:oxModel];
            NSMutableArray *array = [NSMutableArray array];
            
            for (int i = 0; i < checkArray.count; i++) {
                oxSubModel *oxModel = [checkArray objectAtIndex:i];
                [array addObject:[NSString stringWithFormat:@"%d", oxModel.oxValue]];
            }
            
            CGFloat max = [[array valueForKeyPath:@"@max.floatValue"] floatValue];
            CGFloat min = [[array valueForKeyPath:@"@min.floatValue"] floatValue];
            float   avg = [[array valueForKeyPath:@"@avg.floatValue"] floatValue];//求平均值
            tmodel.oxAve         = (NSUInteger)avg;
            tmodel.oxMax         = max;
            tmodel.oxMin         = min;
            tmodel.oxsuArrayAuto = [NSMutableArray arrayWithArray:checkArray];
            [tmodel updateToDB];
            [OxModel updateCheckOX];
            [self selectType:CheckOX];
            checkFinish = YES;
            _type       = CheckOX;
        }
        
        if (SN_PublicClass.checkType == 3 && SN_PublicClass.bpCheckArray) {
            int a = [[SN_PublicClass.bpCheckArray firstObject]intValue];
            int b = [[SN_PublicClass.bpCheckArray lastObject]intValue];
            
            if (a & b) {
            } else {
                return;
            }
            bpSubModel *model = [[bpSubModel alloc] init];
            model.count = [NSDate getCurrentMinTotal]/15;
            int currentMin = [NSDate getCurrentMinTotal];
            model.countMins = currentMin;
            NSDate *nowDate      = [NSDate date];//现在的UTC时间也可以写成[[NSDate alloc] init];
            NSDate *startOfToday = [[NSCalendar currentCalendar] startOfDayForDate:nowDate];
            
            NSDate   *date = [startOfToday dateByAddingTimeInterval:currentMin *60];
            NSString *time = [date dateToString];
            model.updateTime = time;
            model.SBPValue   = [[SN_PublicClass.bpCheckArray firstObject] integerValue];
            model.DBPValue   = [[SN_PublicClass.bpCheckArray lastObject] integerValue];
            //            model.data       = [NSString stringWithFormat:@"%d,%d", model.SBPValue, model.DBPValue];
            
            BpModel        *tmodel     = SN_PublicClass.todayBp;
            NSMutableArray *checkArray = [NSMutableArray arrayWithArray:tmodel.bpSuArray];
            [checkArray addObject:model];
            
            NSMutableArray *sbpArr  = [NSMutableArray array];
            NSMutableArray *dbpArr  = [NSMutableArray array];
            int            sbpTotal = 0;
            int            dbpTotal = 0;
            for (int i = 0; i < checkArray.count; i++) {
                bpSubModel *bpModel = [checkArray objectAtIndex:i];
                [sbpArr addObject:StrByInt(bpModel.SBPValue)];
                [dbpArr addObject:StrByInt(bpModel.DBPValue)];
                sbpTotal += bpModel.SBPValue;
                dbpTotal += bpModel.DBPValue;
            }
            
            tmodel.SBPAve    = sbpTotal / sbpArr.count;
            tmodel.DBPAve    = dbpTotal / dbpArr.count;
            tmodel.bpSuArray = [NSMutableArray arrayWithArray:checkArray];
            [tmodel updateToDB];
            [BpModel updateCheckBP];
            checkFinish = YES;
            _type       = CheckBP;
        }
        
        // 如果没有检测到值 提示体检异常
        if (!checkFinish) {
            SHOWMBProgressHUD(SN_Text(@"体检异常"), nil, nil, NO, 2.0);
        } else {
            [self selectType:_type];
        }
    }
}

- (void)endClick {
    _selectView.isCheck    = NO;
    SN_PublicClass.isCheck = NO;
    _count                 = SN_USER.checkTime;
    _stepCircle.progress   = 1.0 *_count/SN_USER.checkTime;
    _checkLabelInfo.text   = [NSString stringWithFormat:@"%@ %d", SN_Text(@"体检时长:"), _count];
    [_timer invalidate];
    _timer = nil;
    [KK_BLESend sendHeartStatus:NO type:SN_PublicClass.checkType WithUpdate:^(id object, KKBLEAcceptType type) {
    }];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self endClick];
}

- (void)tapGestureHandle {
}

- (void)selectType:(CheckType)type {
    SN_PublicClass.checkType = type;
    _type                    = type;
    _modelArray              = [NSMutableArray array];
    if (SN_PublicClass.checkType == CheckHeart) {
        HeartModel *model = [HeartModel getHeartModelFromDBWith:SN_PublicClass.selectDate];
        NSString   *sss   = SN_Text(@"暂无数据");
        if (model.heartSuArrayCheck) {
            heartSubModel *hmodel = [model.heartSuArrayCheck lastObject];
            sss = [NSString stringWithFormat:@"%d", hmodel.heartValue];
        }
        _stepCircle.ValueLabel.text = sss;
        
        for (int k = 0; k < model.heartSuArrayCheck.count; k++) {
            heartSubModel *hmodel = [model.heartSuArrayCheck objectAtIndex:k];
            NSString      *time   = [hmodel.updateTime componentsSeparatedByString:@" "][1];
            time = [NSString subString:time withToIndex:5];
            [_modelArray addObject:[NSString stringWithFormat:@"     %@                                                  %d bpm", time, hmodel.heartValue]];
        }
    }
    if (SN_PublicClass.checkType == CheckOX) {
        NSString *sss   = SN_Text(@"暂无数据");
        OxModel  *model = [OxModel getOxModelFromDBWith:SN_PublicClass.selectDate];
        if (model.oxsuArrayAuto) {
            oxSubModel *oxmodel = [model.oxsuArrayAuto lastObject];
            sss = [NSString stringWithFormat:@"%d", oxmodel.oxValue];
        }
        _stepCircle.ValueLabel.text = sss;
        
        for (int k = 0; k < model.oxsuArrayAuto.count; k++) {
            oxSubModel *oxmodel = [model.oxsuArrayAuto objectAtIndex:k];
            NSString   *time    = [oxmodel.updateTime componentsSeparatedByString:@" "][1];
            time = [NSString subString:time withToIndex:5];
            [_modelArray addObject:[NSString stringWithFormat:@"     %@                                                   %d %%", time, oxmodel.oxValue]];
        }
    }
    if (SN_PublicClass.checkType == CheckBP) {
        NSString *sss   = SN_Text(@"暂无数据");
        BpModel  *model = [BpModel getBpModelFromDBWith:SN_PublicClass.selectDate];
        if (model.bpSuArray) {
            bpSubModel *bpmodel = [model.bpSuArray lastObject];
            sss = [NSString stringWithFormat:@"%d ~ %d", bpmodel.SBPValue, bpmodel.DBPValue];
        }
        _stepCircle.ValueLabel.text = sss;
        for (int k = 0; k < model.bpSuArray.count; k++) {
            bpSubModel *bpmodel = [model.bpSuArray objectAtIndex:k];
            NSString   *time    = [bpmodel.updateTime componentsSeparatedByString:@" "][1];
            time = [NSString subString:time withToIndex:5];
            [_modelArray addObject:[NSString stringWithFormat:@"     %@               SBP:%d mmHg     DBP:%d mmHg", time, bpmodel.SBPValue, bpmodel.DBPValue]];
        }
    }
    _modelArray = (NSMutableArray *)[[_modelArray reverseObjectEnumerator] allObjects];
    [_tableView reloadData];
}

// 分享
- (void)shareButtonClick {
    UIGraphicsBeginImageContext(CGSizeMake(screenWidth, screenHeight));
    
    [[UIApplication sharedApplication].delegate.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSMutableArray *items = [[NSMutableArray alloc] init];
    [items addObject:image];
    
    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
    NSMutableArray           *excludedActivityTypes  = [NSMutableArray arrayWithArray:@[
                                                                                        UIActivityTypeMessage, UIActivityTypeMail, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr, UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo, UIActivityTypeAirDrop,
                                                                                        UIActivityTypeOpenInIBooks]];
    activityViewController.excludedActivityTypes = excludedActivityTypes;
    
    [self presentViewController:activityViewController animated:YES completion:nil];
    activityViewController.completionWithItemsHandler = ^(UIActivityType __nullable activityType, BOOL completed, NSArray *__nullable returnedItems, NSError *__nullable activityError){
    };
}

// 详情
- (void)detailButtonClick {
    if (_selectView.isCheck) {
        SHOWMBProgressHUD(SN_Text(@"正在体检"), nil, nil, NO, 2.0);
        return;
    }
    
    if (SN_PublicClass.checkType == 1) {
        SNHearDetailVc *vc = [[SNHearDetailVc alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    } else if (SN_PublicClass.checkType == 2) {
        SNOxDetailVc *vc = [[SNOxDetailVc alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    } else if (SN_PublicClass.checkType == 3) {
        SNBpDetailVc *vc = [[SNBpDetailVc alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)backView {
    if (_selectView.isCheck) {
        SHOWMBProgressHUD(SN_Text(@"正在体检"), nil, nil, NO, 2.0);
        return;
    }
    [self.navigationController popViewControllerAnimated:YES];
}

// 心率 血氧  血压
- (void)updateView:(KKBLEAcceptType)type {
    if (!_selectView.isCheck) {
        return;
    }
    if (type == KKBLEAcceptTypeRealtimeHeart) {
        _stepCircle.ValueLabel.text = SN_PublicClass.heartValueString;
    }
    if (type == KKBLEAcceptTypeRealtimeOX) {
        _stepCircle.ValueLabel.text = SN_PublicClass.oxValueString;
    }
    if (type == KKBLEAcceptTypeRealtimeBP) {
        _count                      = 1;
        _stepCircle.ValueLabel.text = SN_PublicClass.bpValueString;
        [KK_BLESend sendHeartStatus:NO type:SN_PublicClass.checkType WithUpdate:^(id object, KKBLEAcceptType type) {
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

